import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
import EslintPlugin from '@nabla/vite-plugin-eslint'
import { PlusProComponentsResolver } from '@plus-pro-components/resolver'
import Vue from '@vitejs/plugin-vue'
import VueJsx from '@vitejs/plugin-vue-jsx'
import { resolve } from 'node:path'
import { visualizer } from 'rollup-plugin-visualizer'
import AutoImport from 'unplugin-auto-import/vite'
import ElementPlus from 'unplugin-element-plus/vite'
import TurboConsole from 'unplugin-turbo-console/vite'
import {
    ElementPlusResolver,
    VueUseComponentsResolver,
    VueUseDirectiveResolver,
} from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { defineConfig, loadEnv } from 'vite'
import { ViteEjsPlugin } from 'vite-plugin-ejs'
// import progress from 'vite-plugin-progress'
import PurgeIcons from 'vite-plugin-purge-icons'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

export const useApplicationConfig = defineConfig(({ mode }) => {
    const root = process.cwd()
    const pathResolve = (pathname: string) => resolve(root, '.', pathname)

    const env = loadEnv(mode, root)

    return {
        base: '/',
        build: {
            cssCodeSplit: !(env.VITE_USE_CSS_SPLIT === 'false'),
            outDir: env.VITE_OUT_DIR || 'dist',
            // brotliSize: false,
            rollupOptions: {
                // 拆包
                output: {
                    manualChunks: {
                        'vue-core': ['vue', 'vue-router'],
                        'pinia': ['pinia'],
                        'i18n': ['vue-i18n'],
                        'element-plus': ['element-plus'],
                        'plus-pro-components': ['plus-pro-components'],
                        // 'element-plus': ['element-plus'],
                        // 'plus-pro-components': ['plus-pro-components'],
                        // 'vue-chunks': ['vue', 'vue-router', 'pinia', 'vue-i18n'],
                    },
                },
                plugins: env.VITE_USE_BUNDLE_ANALYZER === 'true' ? [visualizer()] : undefined,
            },
            sourcemap: env.VITE_SOURCEMAP === 'true',
            target: 'es2015',
        },
        css: {
            preprocessorOptions: {
                less: {
                    additionalData: '@import "@fl/styles/variables.module.less";',
                    javascriptEnabled: true,
                },
                scss: {
                    additionalData: `@use "@/plugins/element-plus/override-element.scss" as *;`,
                    javascriptEnabled: true,
                },
            },
        },
        esbuild: {
            drop: env.VITE_DROP_DEBUGGER === 'true' ? ['debugger'] : undefined,
            pure: env.VITE_DROP_CONSOLE === 'true' ? ['console.log'] : undefined,
        },
        optimizeDeps: {
            exclude: ['node_modules/.vite'],
            include: [
                'vue',
                'vue-router',
                'vue-types',
                'element-plus',
                'plus-pro-components',
                '@vueuse/core',
                'axios',
                'qs',
                'dayjs',
                '@tanstack/vue-query',
                'pinia',
                'pinia-plugin-persistedstate',
                'vxe-table',
                'dayjs/locale/zh-cn',
                'dayjs/plugin/advancedFormat',
                'dayjs/plugin/isLeapYear',
                'dayjs/plugin/isToday',
                'dayjs/plugin/isYesterday',
                'dayjs/plugin/localizedFormat',
                'dayjs/plugin/timezone',
                'dayjs/plugin/updateLocale',
                'dayjs/plugin/utc',
                'element-plus/es/components/alert/style/index',
                'element-plus/es/components/base/style/index',
                'element-plus/es/components/badge/style/index',
                'element-plus/es/components/breadcrumb-item/style/index',
                'element-plus/es/components/breadcrumb/style/index',
                'element-plus/es/components/button/style/index',
                'element-plus/es/components/container/style/index',
                'element-plus/es/components/card/style/index',
                'element-plus/es/components/cascader/style/index',
                'element-plus/es/components/check-tag/style/index',
                'element-plus/es/components/checkbox-group/style/index',
                'element-plus/es/components/checkbox/style/index',
                'element-plus/es/components/col/style/index',
                'element-plus/es/components/color-picker/style/index',
                'element-plus/es/components/config-provider/style/index',
                'element-plus/es/components/date-picker/style/index',
                'element-plus/es/components/dialog/style/index',
                'element-plus/es/components/divider/style/index',
                'element-plus/es/components/drawer/style/index',
                'element-plus/es/components/dropdown-item/style/index',
                'element-plus/es/components/dropdown-menu/style/index',
                'element-plus/es/components/dropdown/style/index',
                'element-plus/es/components/empty/style/index',
                'element-plus/es/components/form-item/style/index',
                'element-plus/es/components/form/style/index',
                'element-plus/es/components/header/style/index',
                'element-plus/es/components/icon/style/index',
                'element-plus/es/components/image/style/index',
                'element-plus/es/components/input-number/style/index',
                'element-plus/es/components/input/style/index',
                'element-plus/es/components/link/style/index',
                'element-plus/es/components/loading/style/index',
                'element-plus/es/components/main/style/index',
                'element-plus/es/components/menu-item/style/index',
                'element-plus/es/components/menu/style/index',
                'element-plus/es/components/message-box/style/index',
                'element-plus/es/components/message/style/index',
                'element-plus/es/components/notification/style/index',
                'element-plus/es/components/option/style/index',
                'element-plus/es/components/pagination/style/index',
                'element-plus/es/components/popover/style/index',
                'element-plus/es/components/progress/style/index',
                'element-plus/es/components/radio-group/style/index',
                'element-plus/es/components/radio/style/index',
                'element-plus/es/components/row/style/index',
                'element-plus/es/components/scrollbar/style/index',
                'element-plus/es/components/select/style/index',
                'element-plus/es/components/space/style/index',
                'element-plus/es/components/step/style/index',
                'element-plus/es/components/steps/style/index',
                'element-plus/es/components/sub-menu/style/index',
                'element-plus/es/components/switch/style/index',
                'element-plus/es/components/tab-pane/style/index',
                'element-plus/es/components/table-column/style/index',
                'element-plus/es/components/table-column/style/css',
                'element-plus/es/components/table/style/index',
                'element-plus/es/components/tabs/style/index',
                'element-plus/es/components/tag/style/index',
                'element-plus/es/components/tag/style/css',
                'element-plus/es/components/timeline-item/style/index',
                'element-plus/es/components/timeline/style/index',
                'element-plus/es/components/tooltip/style/index',
                'element-plus/es/components/transfer/style/index',
                'element-plus/es/components/upload/style/index',
                'element-plus/es/components/popconfirm/style/index',
                'element-plus/es/components/backtop/style/index',
                'element-plus/es/components/affix/style/index',
                'element-plus/es/components/statistic/style/index',
                'element-plus/es/components/tree/style/index',
                'element-plus/es/components/tree-v2/style/index',
                'element-plus/es/components/tree-select/style/index',
                'element-plus/es/components/table-v2/style/index',
                'element-plus/es/components/skeleton/style/index',
                'element-plus/es/components/skeleton-item/style/index',
                'element-plus/es/components/collapse/style/index',
                'element-plus/es/components/collapse-item/style/index',
                'element-plus/es/components/collapse-transition/style/index',
                'element-plus/es/components/carousel/style/index',
                'element-plus/es/components/carousel-item/style/index',
                'element-plus/es/components/calendar/style/index',
                'element-plus/es/components/badge/style/index',
                'element-plus/es/components/avatar/style/index',
                'element-plus/es/components/aside/style/index',
                'element-plus/es/components/alert/style/css',
                'element-plus/es/components/base/style/css',
                'element-plus/es/components/badge/style/css',
                'element-plus/es/components/breadcrumb-item/style/css',
                'element-plus/es/components/breadcrumb/style/css',
                'element-plus/es/components/button/style/css',
                'element-plus/es/components/card/style/css',
                'element-plus/es/components/cascader/style/css',
                'element-plus/es/components/check-tag/style/css',
                'element-plus/es/components/checkbox-group/style/css',
                'element-plus/es/components/checkbox/style/css',
                'element-plus/es/components/col/style/css',
                'element-plus/es/components/color-picker/style/css',
                'element-plus/es/components/config-provider/style/css',
                'element-plus/es/components/date-picker/style/css',
                'element-plus/es/components/dialog/style/css',
                'element-plus/es/components/divider/style/css',
                'element-plus/es/components/drawer/style/css',
                'element-plus/es/components/dropdown-item/style/css',
                'element-plus/es/components/dropdown-menu/style/css',
                'element-plus/es/components/dropdown/style/css',
                'element-plus/es/components/empty/style/css',
                'element-plus/es/components/form-item/style/css',
                'element-plus/es/components/form/style/css',
                'element-plus/es/components/icon/style/css',
                'element-plus/es/components/image/style/css',
                'element-plus/es/components/input-number/style/css',
                'element-plus/es/components/input/style/css',
                'element-plus/es/components/link/style/css',
                'element-plus/es/components/loading/style/css',
                'element-plus/es/components/menu-item/style/css',
                'element-plus/es/components/menu/style/css',
                'element-plus/es/components/message-box/style/css',
                'element-plus/es/components/message/style/css',
                'element-plus/es/components/notification/style/css',
                'element-plus/es/components/option/style/css',
                'element-plus/es/components/pagination/style/css',
                'element-plus/es/components/popover/style/css',
                'element-plus/es/components/progress/style/css',
                'element-plus/es/components/radio-group/style/css',
                'element-plus/es/components/radio/style/css',
                'element-plus/es/components/row/style/css',
                'element-plus/es/components/scrollbar/style/css',
                'element-plus/es/components/select/style/css',
                'element-plus/es/components/space/style/css',
                'element-plus/es/components/step/style/css',
                'element-plus/es/components/steps/style/css',
                'element-plus/es/components/sub-menu/style/css',
                'element-plus/es/components/switch/style/css',
                'element-plus/es/components/tab-pane/style/css',
                'element-plus/es/components/table-column/style/css',
                'element-plus/es/components/table/style/css',
                'element-plus/es/components/tabs/style/css',
                'element-plus/es/components/tag/style/css',
                'element-plus/es/components/timeline-item/style/css',
                'element-plus/es/components/timeline/style/css',
                'element-plus/es/components/tooltip/style/css',
                'element-plus/es/components/transfer/style/css',
                'element-plus/es/components/upload/style/css',
                'element-plus/es/components/popconfirm/style/css',
                'element-plus/es/components/backtop/style/css',
                'element-plus/es/components/affix/style/css',
                'element-plus/es/components/statistic/style/css',
                'element-plus/es/components/tree/style/css',
                'element-plus/es/components/tree-v2/style/css',
                'element-plus/es/components/tree-select/style/css',
                'element-plus/es/components/table-v2/style/css',
                'element-plus/es/components/skeleton/style/css',
                'element-plus/es/components/skeleton-item/style/css',
                'element-plus/es/components/collapse/style/css',
                'element-plus/es/components/collapse-item/style/css',
                'element-plus/es/components/collapse-transition/style/css',
                'element-plus/es/components/carousel/style/css',
                'element-plus/es/components/carousel-item/style/css',
                'element-plus/es/components/calendar/style/css',
                'element-plus/es/components/badge/style/css',
                'element-plus/es/components/avatar/style/css',
                'element-plus/es/components/aside/style/css',
                'element-plus/es/components/radio-button/style/css',
                'element-plus/es/components/option-group/style/css',
                'element-plus/es/components/checkbox-button/style/css',
                'element-plus/es/components/rate/style/css',
                'element-plus/es/components/select-v2/style/css',
                'element-plus/es/components/slider/style/css',
                'element-plus/es/components/time-picker/style/css',
                'element-plus/es/components/time-select/style/css',
                'element-plus/es/components/autocomplete/style/css',
                'element-plus/es/components/segmented/style/index',
                'element-plus/es/locale/lang/zh-cn',
                'plus-pro-components/es/components/dialog-form/style/css',
                'plus-pro-components/es/components/pagination/style/css',
            ],
        },
        plugins: [
            AutoImport({
                dirs: ['src/auto-import'],
                dts: 'types/auto-imports.d.ts',
                eslintrc: {
                    enabled: true,
                    filepath: '../../.eslintrc-auto-import.json',
                },
                imports: [
                    'vue',
                    'vue-router',
                    'pinia',
                    {
                        '@tanstack/vue-query': [
                            'useQuery',
                            'useQueries',
                            'useInfiniteQuery',
                            'useMutation',
                            'useQueryClient',
                        ],
                    },
                    {
                        zod: [
                            'z',
                        ],
                    },
                ],
                resolvers: [
                    ElementPlusResolver({
                        importStyle: 'sass',
                    }),
                ],
            }),
            Components({
                dts: 'types/components.d.ts',
                resolvers: [
                    ElementPlusResolver({
                        importStyle: 'sass',
                    }),
                    PlusProComponentsResolver(),
                    VueUseComponentsResolver(),
                    VueUseDirectiveResolver(),
                ],
            }),
            ElementPlus({}),
            TurboConsole({}),
            Vue({
                script: {
                    // 响应性 props 解构
                    // https://github.com/vuejs/rfcs/discussions/502
                    propsDestructure: true,
                },
            }),
            VueJsx(),
            // progress 包会导致华为云构建失败
            // progress(),
            EslintPlugin(),
            VueI18nPlugin({
                compositionOnly: true,
                include: [pathResolve('../../packages/locales/**')],
                runtimeOnly: true,
            }),
            createSvgIconsPlugin({
                iconDirs: [pathResolve('src/assets/svgs')],
                svgoOptions: true,
                symbolId: 'icon-[dir]-[name]',
            }),
            PurgeIcons(),
            ViteEjsPlugin({
                title: env.VITE_APP_TITLE,
            }),
        ],
        resolve: {
            alias: {
                '#': pathResolve('types'),
                '@': pathResolve('src'),
                'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
            },
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
        },
        server: {
            // 允许跨域
            cors: true,
            // 监听所有地址
            host: true,
            // 服务启动时是否自动打开浏览器
            open: true,
            // 自定义代理规则
            proxy: {
                '/fl': {
                    changeOrigin: true,
                    rewrite: (path: string) => path.replace(/^\/fl/, ''),
                    target: env.VITE_API_BASE_PATH,
                },

            },
            // 预热文件以提前转换和缓存结果，降低启动期间的初始页面加载时长并防止转换瀑布
            warmup: {
                clientFiles: ['index.html', 'src/{pages,components}/*'],
            },
        },
    }
})
